﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Recherche_Concert
{
    public class Venue
    {
        #region Champs
        private static string url_venueGetEvents = "http://ws.audioscrobbler.com/2.0/?method=venue.getevents&api_key=27c34d179daebafc88cf103c35a96f67&venue=#idVenue#";
        private static string url_venueSearch = "http://ws.audioscrobbler.com/2.0/?method=venue.search&api_key=27c34d179daebafc88cf103c35a96f67&venue=#nameVenue#";
        //un id, un nom, un lien Uri de l’image, un lien Uri du site Web et un numéro de téléphone
        private string _id = "inconnu";

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
        private string _nom = "inconnu";

        public string Nom
        {
            get { return _nom; }
            set { _nom = value; }
        }
        private string _lienUriImage = "inconnu";

        public string LienUriImage
        {
            get { return _lienUriImage; }
            set { _lienUriImage = value; }
        }
        private string _lienUriSiteWeb = "inconnu";

        public string LienUriSiteWeb
        {
            get { return _lienUriSiteWeb; }
            set { _lienUriSiteWeb = value; }
        }
        private string _tel = "inconnu";

        public string Tel
        {
            get { return _tel; }
            set { _tel = value; }
        }
        #endregion

        #region Constructeurs
        public Venue()
        {

        }
        public Venue(string Id)
        {
            this.Id = Id;
        }
        public Venue(string Id,string Nom,string LienUriImage,string LienUriSiteWeb,string Tel)
        {
            this.Id = Id;
            this.Nom = Nom;
            this.LienUriImage = LienUriImage;
            this.LienUriSiteWeb = LienUriSiteWeb;
            this.Tel = Tel;
        }
        #endregion

        #region Méthodes
        public static List<Venue> getAllVenueByName(string name)
        {
            string urlVenue = url_venueSearch.Replace("#nameVenue#", name);
            List<Venue> lesVenues = new List<Venue>();

            IEnumerable<XElement> xlesVenues = XDocument.Load(urlVenue).Root.Element("results").Element("venuematches").Elements("venue");

            foreach (XElement xVenue in xlesVenues)
            {
                string input=xVenue.Element("phonenumber").NextNode.NextNode.NextNode.ToString();
                int deb = input.IndexOf(">")+1;
                int fin = input.IndexOf("jpg")-17;
                string sub = input.Substring(deb,fin);
                Venue uneVenue = new Venue(xVenue.Element("id").Value,xVenue.Element("name").Value,sub,xVenue.Element("website").Value,xVenue.Element("phonenumber").Value);

                lesVenues.Add(uneVenue);
            }
            return lesVenues;
        }
        #endregion
    }
}
